<?php namespace Common\Model;
use Hdphp\Model\Model;
class Brand extends Model{
	protected $table = 'brand';
	protected $validate = array(
		array('bname','required','品牌名称必须填写',3,3),
		array('sort','required','品牌排序必须填写',3,3)	
	);
	protected $auto = array(
		//缩略图
		array('logo','getLogo','method',3,3)
	);
	//缩略图方法
	public function getLogo(){
	    //如果没有上传
	    if($_FILES['logo']['error'] == 4){
	    		$this->error = 'logo没有上传';
			return;
	    }
		//上传
		//上传logo地址和类型
		$files = Upload::path('./Upload' . date('ymd'))->type('jpg,png,gif')->make();
		//logo上传失败,将错误信息压入当前模型的error
		if(!$files){
			$this->error = Upload::getError();
			return;
		}
		//缩略
		$path = $files[0]['path'];
		$type = strrchr($path, '.');
		$newPath = str_replace($type, '_logo' . $type, $path);
		Image::thumb($path,$newPath,100,100,6);
		return $newPath;
	}
	//添加model
	public function store(){
	    if(!$this->create()){
	    		return FALSE;
	    }
		if($this->error){
			return FALSE;
		}
		$this->add();
		return TRUE;
	}
	//编辑
	public function edit(){
	    if(!$this->create()) return FALSE;
		if($this->error) return FALSE;
		$this->save();
		return TRUE;
	}
}

 ?>